//
//  SettingViewController.m
//  网络FM
//
//  Created by dengzhicheng on 15/5/26.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//




#import "SettingViewController.h"

#import "SettingButton.h"



#import "CopyrightNoticeViewController.h"
#import "AboutUSViewController.h"

#import <KVNProgress.h>
#import  <Reachability.h>

#import "MineViewController.h"
#import "SettingTableViewCell.h"
#import "WeiboSDK.h"
@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate, WBHttpRequestDelegate, UIAlertViewDelegate>


@property (nonatomic, retain)UITableView *table;
@end

@implementation SettingViewController




- (void)dealloc
{
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self bigView];
    [self tableView];
     UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:recognizer];
    [recognizer release];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
}
- (void)bigView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    [view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:view];
    [view release];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, 20, 50, 50)];
    title.text = @"设置";
    title.font = [UIFont boldSystemFontOfSize:24];
    [view addSubview:title];
    [title release];
}
- (void)tableView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    _table.dataSource = self;
    _table.delegate = self;
    [self.view addSubview:_table];
    [_table release];
}
#pragma mark -
#pragma mark Section
//定义分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
        return 6;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"likeCell";
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *state = [defaults objectForKey:@"state"];
    
    
    if (!cell) {
        cell = [[SettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    if (indexPath.section == 0) {
        [cell.titleLable setText:@"夜间模式"];
        cell.modleSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 100, 14, 100, 50)];
        if ([state isEqualToString:@"1"]) {
            [cell.modleSwitch setOn:YES];
            
        }
        else {
            [cell.modleSwitch setOn:NO];
        }
        [cell.contentView addSubview:cell.modleSwitch];
        [cell.modleSwitch  addTarget:self action:@selector(switch1Teg:) forControlEvents:UIControlEventValueChanged];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else {
        if (indexPath.row == 0) {
            [cell.titleImageView setImage:[UIImage imageNamed:@"icon2.png"]];
            [cell.mainLable setText:@"清理缓存"];
        } else if (indexPath.row == 1){
            [cell.titleImageView setImage:[UIImage imageNamed:@"icon3.png"]];
            [cell.mainLable setText:@" 赞一个"];
        } else if (indexPath.row == 2){
            [cell.titleImageView setImage:[UIImage imageNamed:@"icon4.png"]];
            [cell.mainLable setText:@"关于我们"];
        } else if (indexPath.row == 3){
            [cell.titleImageView setImage:[UIImage imageNamed:@"icon5.png"]];
            [cell.mainLable setText:@"  版权"];
        } else if (indexPath.row == 4){
            [cell.titleImageView setImage:[UIImage imageNamed:@"icon6.png"]];

            [cell.mainLable setText:@"  返回"];
        } else if (indexPath.row == 5){
            [cell.titleImageView setImage:[UIImage imageNamed:@"icon7.png"]];
            [cell.mainLable setText:@"  注销"];

        }
    }
    
    return cell;
}

-(void)switch1Teg:(UISwitch *)Switch
{
    if (Switch.on) {
        self.view.window.alpha = 0.418;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@"1" forKey:@"state" ];
        [defaults synchronize];
    }else{
        self.view.window.alpha = 1;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@"0" forKey:@"state" ];
        [defaults synchronize];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UIScreen mainScreen].bounds.size.width / 6;
}
 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSFileManager *manager = [NSFileManager defaultManager];
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *path = [array lastObject];
        NSString *str = [NSString stringWithFormat:@"%@/Caches", path];
        [manager removeItemAtPath:str error:nil];
        [KVNProgress showSuccessWithStatus:@"清除成功"];
        
    } else if (indexPath.row == 1) {
//        NSLog(@"赞一个");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/jie-zou-da-shi/id493901993?mt=8"]];
    } else if (indexPath.row == 2) {
        
        AboutUSViewController *us = [[AboutUSViewController alloc]init];
        [us setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentViewController:us animated:YES completion:nil];
    }else if (indexPath.row == 3) {
        CopyrightNoticeViewController *p = [[CopyrightNoticeViewController alloc]init];
        [p setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentViewController:p animated:YES completion:nil];
    }else if (indexPath.row == 4) {
//        NSLog(@"退出");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"huifu" object:nil];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (indexPath.row == 5)
    {
        // 账号注销
        MineViewController *myDelegate =(MineViewController*)[[UIApplication sharedApplication] delegate];
        [WeiboSDK logOutWithToken:myDelegate.wbtoken delegate:self withTag:@"user1"];
        
        
        // 创建NSUserDefaults对象
        NSUserDefaults *loginState = [NSUserDefaults standardUserDefaults];
        
        // 添加存储内容
        [loginState setObject:@"0" forKey:@"loginState"];
        // 同步存储
        [loginState synchronize];
    }
    [self.table deselectRowAtIndexPath:[self.table indexPathForSelectedRow] animated:YES];
}

#pragma mark -
#pragma WBHttpRequestDelegate

- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result
{
    UIAlertView *alert = nil;
    
    alert = [[UIAlertView alloc] initWithTitle:@"已注销!" message:[NSString stringWithFormat:@""] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.delegate = self;
    [alert show];
    [alert release];
//    NSLog(@"注销操作");
    
    // 消息中心将字典传给mine视图控制器
    [[NSNotificationCenter defaultCenter] postNotificationName:@"logout" object:nil];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    // 抽屉状态恢复
    [[NSNotificationCenter defaultCenter] postNotificationName:@"huifu" object:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error;
{
    UIAlertView *alert = nil;
    
    alert = [[UIAlertView alloc] initWithTitle:@"请求异常"
                                       message:[NSString stringWithFormat:@"%@",error]
                                      delegate:nil
                             cancelButtonTitle:@"确定"
                             otherButtonTitles:nil];
    [alert show];
    [alert release];
}






-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {

        [self dismissViewControllerAnimated:YES completion:nil];
        
        // 抽屉状态恢复
        [[NSNotificationCenter defaultCenter] postNotificationName:@"huifu" object:nil];
    }
}



@end
